package com.acme.bankapp.domain.bank;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.acme.bankapp.domain.bank.listeners.ClientRegistrationListener;

/**
 * Bank class contains all clients of bank
 * @author Artem Nikitin
 *
 */
public class Bank implements Serializable {

	private static final long serialVersionUID = 1L;

	private List<Client> clients = new ArrayList<Client>();
	
	transient private List<ClientRegistrationListener> listeners = new ArrayList<ClientRegistrationListener>();
	
	public Bank() {
		addClientRegistrationListener(new ClientRegistrationListener() {
			
			public void onClientAdded(Client client) {
				System.out.println(new Date());				
			}
		});
	}
	
	public Iterator<Client> clients() {
		return clients.iterator();
	}
	
	public Client getClient(int index) {
		return clients.get(index);
	}
	
	public void addClient(Client client) {
		clients.add(client);
		for (ClientRegistrationListener crl : listeners) {
			crl.onClientAdded(client);
		}
	}
	
	public int getClientCount() {
		return clients.size();
	}
	
	public void addClientRegistrationListener(ClientRegistrationListener crl) {
		listeners.add(crl);
	}
	
	public void removeClientRegistrationListener(ClientRegistrationListener crl) {
		listeners.remove(crl);
	}
}
